home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / inter489.arc / INTPRINT.C < prev    next >
C/C++ Source or Header  |  1989-07-23  |  14KB  |  513 lines

  1. /***********************************************/
  2. /* Copyright (c) 1989  Ralf Brown              */
  3. /* May be freely redistributed provided no fee */
  4. /* is charged, this notice remains intact,     */
  5. /* and any changes are clearly marked as such  */
  6. /***********************************************/
  7. /* Program History:                            */
  8. /*   v1.00  4/23/89  initial public release    */
  9. /*                   with 4/30/89 list         */
  10. /*   v1.10  5/21/89  added -I and -f           */
  11. /***********************************************/
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #define VERSION "1.10"
  17.  
  18. #define MAXLINE 81   /* at most 80 chars per line (plus newline) */
  19. #define MAXPAGE 200  /* at most 200 lines per page */
  20.  
  21. #ifndef FALSE
  22. #define FALSE 0
  23. #endif
  24. #ifndef TRUE
  25. #define TRUE !FALSE
  26. #endif
  27.  
  28. #ifdef __TURBOC__
  29. #  define PROTOTYPES
  30. #  include <stdlib.h>
  31. void _setenvp(void) {} /* don't need the environment, so don't include it */
  32. int isspace(char c) { return (c == ' ' || c == '\t') ; }
  33. #else
  34. /*#define PROTOTYPES  /* uncomment if compiler supports ANSI-style prototypes */
  35. #  include <ctype.h>
  36. char *itoa(num,buf,radix)   /* not everybody has the same itoa() as TurboC */
  37. int num ;                   /* minimal implementation */
  38. char *buf ;
  39. int radix ;
  40. {
  41.    int count = 0 ;
  42.    int i ; 
  43.    char tmp ;
  44.  
  45.    do {
  46.       buf[count++] = '0' + num % radix ;
  47.       num = num / radix ;
  48.    } while (num) ;
  49.    buf[count] = '\0' ;
  50.    if (count > 1)
  51.       for (i = 0 ; i < count / 2 ; i++)
  52.          {
  53.          tmp = buf[i] ;
  54.          buf[i] = buf[count-i-1] ;
  55.          buf[count-i-1] = tmp ;
  56.          }
  57.    return buf ;
  58. }
  59. #endif __TURBOC__
  60.  
  61. /***********************************************/
  62.  
  63. #ifdef PROTOTYPES
  64. void usage(void) ;
  65. void indent_line(FILE *fp) ;
  66. void put_line(FILE *fp, int len) ;
  67. int divider_line(char *line) ;
  68. void fill_buffer(int lines, int lines_per_page) ;
  69. int find_page_break(int lines) ;
  70. void summarize(FILE *summary, int line, int pages_printed) ;
  71. void start_format(char *line) ;
  72. void print_line(char *line) ;
  73. void print_buffer(int first, int last, int lines_per_page, int total_lines, int use_FF) ;
  74. void main(int argc, char **argv) ;
  75. #endif PROTOTYPES
  76.  
  77. /***********************************************/
  78.  
  79. char buffer[MAXPAGE][MAXLINE] ;
  80. char num[6] ;
  81. char summary_line[MAXLINE] ;
  82.  
  83. int pages_printed = 0 ;
  84. int indent = 0 ;
  85. int page_numbers = FALSE ;
  86. int do_summary = FALSE ;
  87. int do_formats = FALSE ;
  88. int IBM_chars = FALSE ;
  89. int echo_format = FALSE ;
  90. FILE *summary ;
  91. FILE *formats ;
  92.  
  93. /***********************************************/
  94.  
  95. void usage()
  96. {
  97.    fputs( "INTPRINT v", stderr ) ;
  98.    fputs( VERSION, stderr ) ;
  99.    fputs( " Copyright (c) 1989  Ralf Brown.  Free for non-commercial use.\n\n", stderr ) ;
  100.    fputs( "Usage: intprint [options] [lines [page_size]] <intlist >output\n", stderr );
  101.    fputs( "\t'lines' defaults to 60\n", stderr ) ;
  102.    fputs( "\tif page_size is given, only linefeeds will be used to advance\n", stderr ) ;
  103.    fputs( "Options:\n", stderr ) ;
  104.    fputs( "\t-p\tcauses pages to be numbered\n", stderr ) ;
  105.    fputs( "\t-nN\tassume N pages have been printed from previous parts\n", stderr ) ;
  106.    fputs( "\t-e\tassume 'elite' mode (96 characters per line)\n", stderr ) ;
  107.    fputs( "\t-iN\tindent output N spaces\n", stderr ) ;
  108.    fputs( "\t-E\tspecifies that the printer is an Epson FX80 or compatible\n", stderr ) ;
  109.    fputs( "\t\t-E overrides -e and forces -i8\n", stderr ) ;
  110.    fputs( "\t-I\tprinter supports IBM graphics characters\n", stderr ) ;
  111.    fputs( "\t-sfile\twrite a one-line-per-function summary to 'file'\n", stderr ) ;
  112.    exit(1) ;
  113. }
  114.  
  115. /***********************************************/
  116.  
  117. void indent_line(fp)
  118. FILE *fp ;
  119. {
  120.    int ind ;
  121.  
  122.    for (ind = 0 ; ind < indent ; ind++)
  123.       fputc( ' ', fp ) ;
  124. }
  125.  
  126. /***********************************************/
  127.  
  128. void put_line(fp,len)
  129. FILE *fp ;
  130. int len ;
  131. {
  132.    int i ;
  133.  
  134.    if (IBM_chars)
  135.       for (i = 0 ; i < len ; i++)
  136.          fputc(196,fp) ;  /* single horizontal line */
  137.    else
  138.       for (i = 0 ; i < len ; i++)
  139.          fputc('-',fp) ;
  140. }
  141.  
  142. /***********************************************/
  143.  
  144. int divider_line(line)
  145. char *line ;
  146. {
  147.    return strncmp(line,"--------",8) == 0 ;
  148. }
  149.  
  150. /***********************************************/
  151.  
  152. void fill_buffer(lines,lines_per_page)
  153. int lines, lines_per_page ;
  154. {
  155.    int i ;
  156.  
  157.    if (lines)
  158.       for (i = lines ; i < lines_per_page ; i++)
  159.          strcpy( buffer[i-lines], buffer[i] ) ;
  160.    else
  161.       lines = lines_per_page ;
  162.    for (i = lines_per_page - lines ; i < lines_per_page ; i++)
  163.       {
  164.       buffer[i][0] = '\0' ;  /* force empty line in case of EOF */
  165.       fgets( buffer[i], sizeof(buffer[i]), stdin ) ;
  166.       }
  167. }
  168.  
  169. /***********************************************/
  170.  
  171. int find_page_break(lines)
  172. int lines ;
  173. {
  174.    int i ;
  175.  
  176.    for (i = 0 ; i < 10 ; i++)
  177.       {
  178.       if (strcmp(buffer[lines-i-1],"\n") == 0 ||
  179.           strlen(buffer[lines-i-1]) == 0 ||
  180.           divider_line(buffer[lines-i-1]))
  181.          return lines - i ;
  182.       }
  183.    return lines ;
  184. }
  185.  
  186. /***********************************************/
  187.  
  188. void summarize(summary, line, pages_printed)
  189. FILE *summary ;
  190. int line, pages_printed ;
  191. {
  192.    char *s ;
  193.    int i ;
  194.    int max_descrip ;
  195.    int len ;
  196.  
  197.    s = buffer[line] ;
  198.    if (strncmp(s, "INT ", 4 ) == 0)   /* start of an entry? */
  199.       {
  200.       summary_line[3] = summary_line[0] = ' ' ;
  201.       summary_line[1] = s[4] ;   /* output interrupt number */
  202.       summary_line[2] = s[5] ;
  203.       summary_line[4] = '\0' ;
  204.       len = 4 ;
  205.       s = buffer[line+1] ;
  206.       while (*s && isspace(*s))
  207.          s++ ;
  208.       if (strncmp(s,"AX",2) == 0)
  209.          i = 4 ;
  210.       else if (strncmp(s,"AH",2) == 0)
  211.          i = 2 ;
  212.       else
  213.          i = 0 ;
  214.       if (i)
  215.          {
  216.          while (*s && *s != '=' )
  217.             s++ ;
  218.          s++ ;  /* skip the equal sign */
  219.          while (*s && isspace(*s))
  220.             s++ ;
  221.          if (strchr("0123456789ABCDEFabcdef",*s) != NULL)
  222.             {
  223.             summary_line[len++] = *s++ ;
  224.             summary_line[len++] = *s++ ;
  225.             summary_line[len++] = ' ' ;
  226.             if (i == 4)
  227.                {
  228.                summary_line[len++] = *s++ ;
  229.                summary_line[len++] = *s ;
  230.                }
  231.             else
  232.                {
  233.                summary_line[len++] = '-' ;
  234.                summary_line[len++] = '-' ;
  235.                }
  236.             summary_line[len++] = ' ' ;
  237.             }
  238.          else
  239.             {
  240.             /* wasn't legal digit, so no numbers */
  241.             strcpy(summary_line+len,"-- -- ") ;
  242.             len += 6 ;
  243.             }
  244.          }
  245.       else
  246.          {
  247.          strcpy(summary_line+len,"-- -- ") ;
  248.          len += 6 ;
  249.          }
  250.       if (page_numbers)
  251.          {
  252.          itoa(pages_printed+1,num,10) ; /* pages_printed not incremented until later */
  253.          for (i = strlen(num) ; i < 3 ; i++)
  254.             summary_line[len++] = ' ' ;
  255.          strcpy(summary_line+len,num) ;
  256.          len += strlen(num) ;
  257.          summary_line[len++] = ' ' ;
  258.          }
  259.       s = buffer[line] + 7 ;  /* find function description */
  260.       while (*s && !isspace(*s))
  261.          s++ ;
  262.       while (*s && isspace(*s))
  263.          s++ ;
  264.       max_descrip = (page_numbers ? MAXLINE-16 : MAXLINE-12) ;
  265.       for (i = 0 ; i < max_descrip && *s && *s != '\n' ; i++)
  266.          summary_line[len++] = *s++ ;
  267.       summary_line[len++] = '\n' ;
  268.       summary_line[len] = '\0' ;
  269.       if (do_summary)
  270.          {
  271.          indent_line(summary) ;
  272.          fputs(summary_line,summary) ;
  273.          }
  274.       }
  275. }
  276.  
  277. /***********************************************/
  278.  
  279. void start_format(line)
  280. char *line ;
  281. {
  282.    indent_line(formats) ;
  283.    put_line(formats,79) ;
  284.    fputc('\n',formats) ;
  285.    indent_line(formats) ;
  286.    fputs(summary_line,formats) ;
  287.    indent_line(formats) ;
  288.    fputc('\t',formats) ;
  289.    fputs(line+10,formats) ;
  290.    fputc('\n',formats) ;
  291.    echo_format = TRUE ;
  292. }
  293.  
  294. /***********************************************/
  295.  
  296. void print_line(line)
  297. char *line ;
  298. {
  299.    if (*line == '\0' || *line == '\n' || divider_line(line))
  300.       echo_format = FALSE ;
  301.    if (*line && *line != '\n')
  302.       {
  303.       indent_line(stdout) ;
  304.       if (divider_line(line))
  305.          {
  306.          put_line(stdout,79) ;
  307.          fputc('\n', stdout) ;
  308.          echo_format = FALSE ;
  309.          }
  310.       else
  311.          {
  312.          fputs(line, stdout) ;
  313.          if (echo_format)
  314.             {
  315.             indent_line(formats) ;
  316.             fputs(line,formats) ;
  317.             }
  318.          }
  319.       }
  320.    else
  321.       {
  322.       fputc('\n', stdout) ;
  323.       echo_format = FALSE ;
  324.       }
  325. }
  326.  
  327. /***********************************************/
  328.  
  329. void print_buffer(first,last,lines_per_page,total_lines,use_FF)
  330. int first, last, lines_per_page, total_lines ;
  331. int use_FF ;
  332. {
  333.    int i, ind ;
  334.  
  335.    for (i = first ; i < last ; i++)
  336.       {
  337.       print_line(buffer[i]) ;
  338.       if (do_summary || do_formats)  /* need summary lines if doing formats */
  339.          summarize(summary,i,pages_printed) ;
  340.       if (do_formats && strncmp(buffer[i],"Format of ",10) == 0)
  341.          start_format(buffer[i]) ;
  342.       }
  343.    pages_printed++ ;
  344.    if (page_numbers)
  345.       {
  346.       for (i = last - first ; i < lines_per_page - 1 ; i++)
  347.          fputc( '\n', stdout ) ;
  348.       indent_line(stdout) ;
  349.       for (ind = 0 ; ind < 38 ; ind++) /* normal indent + 38 spaces */
  350.          fputc( ' ', stdout ) ;
  351.       fputs( "- ", stdout ) ;
  352.       itoa( pages_printed, num, 10 ) ;
  353.       fputs( num, stdout ) ;
  354.       fputs( " -\n", stdout ) ;
  355.       }
  356.    if (use_FF)
  357.       fputc( '\f', stdout ) ;
  358.    else
  359.       for (i = page_numbers?lines_per_page:(last-first) ; i<total_lines ; i++)
  360.          fputc( '\n', stdout ) ;
  361. }
  362.  
  363. /***********************************************/
  364.  
  365. void main(argc,argv)
  366. int argc ;
  367. char *argv[] ;
  368. {
  369.    int lines_per_page = 60 ;
  370.    int total_lines = 66 ;
  371.    int use_FF = TRUE ;
  372.    int Epson_mode = FALSE ;
  373.    int last_line ;
  374.    int body_lines ;
  375.    char *summary_file = NULL ;
  376.    char *formats_file = NULL ;
  377.  
  378.    while (argc >= 2 && argv[1][0] == '-')
  379.       {
  380.       switch (argv[1][1])
  381.          {
  382.          case 'e':
  383.             indent = 8 ;
  384.             break ;
  385.          case 'E':
  386.             Epson_mode = TRUE ;
  387.             break ;
  388.          case 'I':
  389.             IBM_chars = TRUE ;
  390.             break ;
  391.          case 'p':
  392.             page_numbers = TRUE ;
  393.             break ;
  394.          case 'n':
  395.             pages_printed = atoi( argv[1]+2 ) ;
  396.             break ;
  397.          case 'i':
  398.             indent = atoi( argv[1]+2 ) ;
  399.             break ;
  400.          case 's':
  401.             summary_file = argv[1]+2 ;
  402.             break ;
  403.          case 'f':
  404.             formats_file = argv[1]+2 ;
  405.             break ;
  406.          default:
  407.             usage() ;
  408.          }
  409.       argv++ ;
  410.       argc-- ;
  411.       }
  412.    if (summary_file && *summary_file)
  413.       if ((summary = fopen( summary_file, pages_printed ? "a":"w" )) != NULL)
  414.          do_summary = TRUE ;
  415.       else
  416.          fputs( "unable to open summary file\n", stderr ) ;
  417.    if (formats_file && *formats_file)
  418.       if ((formats = fopen( formats_file, pages_printed ? "a":"w" )) != NULL)
  419.          do_formats = TRUE ;
  420.       else
  421.          fputs( "unable to open formats file\n", stderr ) ;
  422.    if (argc >= 2)
  423.       lines_per_page = atoi(argv[1]) ;
  424.    if (argc == 3)
  425.       {
  426.       total_lines = atoi(argv[2]) ;
  427.       use_FF = FALSE ;
  428.       }
  429.    if (total_lines < lines_per_page)
  430.       {
  431.       total_lines = lines_per_page ;
  432.       use_FF = TRUE ;
  433.       }
  434.    if (argc > 3 || lines_per_page == 0) /* too many or non-numeric first param */
  435.       usage() ;
  436.    if (lines_per_page < 20 || lines_per_page > MAXPAGE)
  437.       {
  438.       fputs( "Surely you jest!  At least 20 and at most 200 lines per page.\n\n", stderr ) ;
  439.       usage() ;
  440.       }
  441. #ifdef __TURBOC__
  442.    setvbuf(stdin,NULL,_IOFBF,10240) ;  /* use larger disk buffers */
  443.    setvbuf(stdout,NULL,_IOFBF,10240) ; /* for better performance */
  444.    if (do_summary)
  445.       setvbuf(summary,NULL,_IOFBF,4096) ;
  446.    if (do_formats)
  447.       setvbuf(formats,NULL,_IOFBF,4096) ;
  448. #endif __TURBOC__
  449.    if (do_summary && pages_printed == 0)
  450.       {           /* create header, but only on first part */
  451.       indent_line(summary) ;
  452.       fputs("\t\t\t\tInterrupt Summary\n",summary) ;
  453.       indent_line(summary) ;
  454.       fputs("\t\t\t\t", summary) ;
  455.       put_line(summary,17) ;
  456.       fputs("\n\n",summary) ;
  457.       indent_line(summary) ;
  458.       fputs("INT AH AL", summary) ;
  459.       if (page_numbers)
  460.          fputs(" Page", summary) ;
  461.       fputs("\t\t\tDescription\n", summary) ;
  462.       indent_line(summary) ;
  463.       put_line(summary,79) ;
  464.       fputc('\n', summary) ;
  465.       }
  466.    if (do_formats && pages_printed == 0)
  467.       {           /* create header, but only on first part */
  468.       indent_line(formats) ;
  469.       fputs("\t\t\tData Structure Formats\n", formats) ;
  470.       indent_line(formats) ;
  471.       fputs("\t\t\t", formats) ;
  472.       put_line(formats,22) ;
  473.       fputs("\n\n", formats) ;
  474.       indent_line(formats) ;
  475.       fputs("INT AH AL", formats) ;
  476.       if (page_numbers)
  477.          fputs(" Page", formats) ;
  478.       fputs("\t\tData Structure\n", formats) ;
  479.       }
  480.    if (Epson_mode)
  481.       {
  482.       indent = 0 ;  /* -E overrides -e and -i */
  483.       fputs( "\033M\033l\007", stdout ) ;
  484.       }
  485.    last_line = 0 ;
  486.    if (page_numbers)
  487.       body_lines = lines_per_page - 2 ;
  488.    else
  489.       body_lines = lines_per_page ;
  490.    while (!feof(stdin))
  491.       {
  492.       fill_buffer(last_line,body_lines) ;
  493.       last_line = find_page_break(body_lines) ;
  494.       print_buffer(0,last_line,lines_per_page,total_lines,use_FF) ;
  495.       }
  496.    if (last_line < body_lines)
  497.       print_buffer(last_line,body_lines,lines_per_page,total_lines,use_FF) ;
  498.    if (Epson_mode)
  499.       {
  500.       fputs( "\033M\033l", stdout ) ;  /* cancel Elite mode and indent */
  501.       fputc( '\0', stdout ) ;
  502.       }
  503.    fflush(stdout) ;
  504.    itoa( pages_printed, num, 10 ) ;
  505.    fputs( num, stderr ) ;
  506.    fputs( " pages\n", stderr) ;
  507.    if (do_summary)
  508.       fclose(summary) ;
  509.    if (do_formats)
  510.       fclose(formats) ;
  511.    exit(0) ;
  512. }
  513.